﻿using System;
using InvoiceChecking.Data;
using InvoiceChecking.Entities;
using Microsoft.Reporting.WebForms;
using WebCommon;

public partial class Admin_ReportPaymentVoucher : System.Web.UI.Page
{
    private DSPaymentVoucher.PaymentDetailDataTable dsPaymentDetail;
    protected void Page_Load(object sender, EventArgs e)
    {
        ReportViewer1.LocalReport.SubreportProcessing += LocalReport_SubreportProcessing;
        var payments = (TList<Payment>)Session["Payment"];
        if (payments != null)
        {
            DataRepository.PaymentProvider.DeepLoad(payments, true);
            var dsPayment = new DSPaymentVoucher.PaymentDataTable();
            dsPaymentDetail = new DSPaymentVoucher.PaymentDetailDataTable();
            foreach (var payment in payments)
            {
                var paymentRow = dsPayment.NewPaymentRow();
                paymentRow.TitleVietnamese = payment.PaymentTypeIdSource.VietnameseTitle;
                paymentRow.TitleEnglish = payment.PaymentTypeIdSource.EnglishTitle;
                paymentRow.PaymentID = payment.PaymentId;
                paymentRow.PaymentDate = payment.PaymentDate;
                paymentRow.ReceivedBy = payment.ReceivedBy;
                paymentRow.ExchangeRate = payment.ExchangeRate.Value;
                paymentRow.Address = payment.Address;
                paymentRow.Description = payment.Description;
                //paymentRow.Amount = payment.Amount;
                paymentRow.LookUpCode = payment.PaymentTypeIdSource.LookUpCode;
                paymentRow.PreparedBy = payment.PreparedBy;
                paymentRow.Account = payment.PaymentTypeIdSource.AccountNumber;
                paymentRow.Address = payment.Address;
                paymentRow.PaymentTypeId = payment.PaymentTypeId;
                paymentRow.IsCompleted = payment.IsCompleted;
                decimal Amount = 0;
                foreach (var paymentDetail in payment.PaymentDetailCollection)
                {
                    var paymentDetailRow = dsPaymentDetail.NewPaymentDetailRow();
                    paymentDetailRow.PaymentID = paymentDetail.PaymentId;
                    paymentDetailRow.PaymentDetailId = paymentDetail.PaymentDetailId;
                    var sunAccountCodes = DataRepository.SunAccountCodeProvider.Get("Accnt_Code='" + paymentDetail.AccountCode.Trim() + "'", "");
                    paymentDetailRow.Description = sunAccountCodes[0].AccntName;
                    paymentDetailRow.AccountCode = paymentDetail.AccountCode;
                    paymentDetailRow.DebitCredit = paymentDetail.DebitCredit;

                    paymentDetailRow.USDAmount = paymentDetail.MoneyType == "USD"
                                                     ? paymentDetail.Amount.Value
                                                     : paymentDetail.Amount.Value / paymentRow.ExchangeRate;
                    paymentDetailRow.VNDAmount = paymentDetail.MoneyType == "USD"
                                                     ? paymentDetail.Amount.Value * paymentRow.ExchangeRate
                                                     : paymentDetail.Amount.Value;
                    if (paymentDetail.DebitCredit == "Credit")
                        if (payment.PaymentTypeIdSource.Currency == "VND")
                            Amount += paymentDetailRow.VNDAmount;
                        else
                            Amount += paymentDetailRow.USDAmount;

                    else
                        if (payment.PaymentTypeIdSource.Currency == "VND")
                            Amount -= paymentDetailRow.VNDAmount;
                        else
                            Amount -= paymentDetailRow.USDAmount;
                    paymentDetailRow.T1 = paymentDetail.T1;
                    paymentDetailRow.T2 = paymentDetail.T2;
                    paymentDetailRow.T3 = paymentDetail.T3;
                    paymentDetailRow.T4 = paymentDetail.T4;
                    dsPaymentDetail.AddPaymentDetailRow(paymentDetailRow);
                }
                payment.IsDebit = Amount <= 0;
                Amount = Math.Abs(Amount);
                payment.Amount = Math.Round(Amount, 2);
                DataRepository.PaymentProvider.Update(payment);
                paymentRow.Amount = Amount;
                paymentRow.Currency = payment.PaymentTypeIdSource.Currency;
                paymentRow.DebitCredit = payment.IsDebit == true ? "C" : "D";
                string amountInWord;
                if (paymentRow.Currency == "VND")
                    amountInWord = String.Format("{0}VND and Zero USD", SharedFunction.ChangeNumericToWords(paymentRow.Amount));
                else
                {
                    amountInWord = String.Format("Zero VND and {0}USD", SharedFunction.ChangeNumericToWords(Math.Round(paymentRow.Amount, 2)));
                }
                paymentRow.AmountInWord = amountInWord;
                dsPayment.AddPaymentRow(paymentRow);

            }
            ReportViewer1.LocalReport.DataSources["DSPaymentVoucher_Payment"].Value = dsPayment;
            ReportViewer1.DataBind();
        }
        else
        {
            Response.Redirect("Payment.aspx", true);
        }
    }

    void LocalReport_SubreportProcessing(object sender, SubreportProcessingEventArgs e)
    {
        e.DataSources.Add(new ReportDataSource("PaymentDetail", dsPaymentDetail));
    }
}
